package com.xiyi.shouxin.dao.user;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import com.xiyi.shouxin.dao.BaseRepository;
import com.xiyi.shouxin.dao.JdbcPreparedStatement;
import com.xiyi.shouxin.entity.user.PiciRecord;
import com.xiyi.shouxin.entity.user.mapper.PiciRecordMapper;
import com.xiyi.shouxin.exception.CommonException;
import com.xiyi.shouxin.exception.ErrorCode;

@Repository
public class PiciRecordRepository extends BaseRepository<PiciRecord> {
    public PiciRecordRepository() {
        super();
        super.stat = new JdbcPreparedStatement<>(PiciRecord.class);
    }

    public void finishByAdminId(Long adminId) throws Exception {
        if (adminId == null) {
            throw new CommonException(ErrorCode.INVALIDPARAMTER);
        }
        StringBuilder sb = new StringBuilder("UPDATE ");
        sb.append(stat.GetTableName());
        sb.append(" SET ");
        sb.append(stat.GetColumnName("isOver"));
        sb.append(" = 1");
        sb.append(" WHERE ");
        sb.append(stat.GetColumnName("adminId"));
        sb.append(" = ?");
        template.update(sb.toString(), adminId);
    }

    public PiciRecord findLastNotOverByAdminId(Long adminId) throws CommonException {
        if (adminId == null) {
            throw new CommonException(ErrorCode.INVALIDPARAMTER);
        }
        StringBuilder sb = new StringBuilder("SELECT ");
        sb.append(stat.GetQueryColumns());
        sb.append(" FROM ");
        sb.append(stat.GetTableName());
        sb.append(" WHERE ");
        sb.append(stat.GetColumnName("adminId"));
        sb.append(" = ? AND ");
        sb.append(stat.GetColumnName("isOver"));
        sb.append(" = 0 ORDER BY ");
        sb.append(stat.GetColumnName("createdAt"));
        sb.append(" DESC LIMIT 1");
        try {
            PiciRecord pici = template.queryForObject(sb.toString(), new Object[] { adminId }, new PiciRecordMapper());
            return pici;
        } catch (EmptyResultDataAccessException e) {

        }
        return null;
    }

    public List<PiciRecord> findByMultiConditionsByPage(Long adminId, Date startTime, Date endTime,
            Sort sort,
            int page, int size) throws Exception {
        List<Object> args = new ArrayList<>();
        List<String> conditions = new ArrayList<>();
        if (adminId != null) {
            StringBuilder sb = new StringBuilder(stat.GetColumnName("adminId"));
            sb.append(" IS NULL OR ");
            sb.append(stat.GetColumnName("adminId"));
            sb.append(" = ? ");
            conditions.add(sb.toString());
            args.add(adminId);
        }
        if (startTime != null) {
            conditions.add(stat.GetColumnName("createdAt") + " >= ? ");
            args.add(startTime);
        }
        if (endTime != null) {
            conditions.add(stat.GetColumnName("createdAt") + " <= ? ");
            args.add(endTime);
        }
        return findByMultiConditionsByPage(conditions, args, sort, page, size, new PiciRecordMapper());
    }

    public Integer countByMultiConditions(Long adminId, Date startTime, Date endTime) {
        List<Object> args = new ArrayList<>();
        List<String> conditions = new ArrayList<>();
        if (adminId != null) {
            StringBuilder sb = new StringBuilder(stat.GetColumnName("adminId"));
            sb.append(" IS NULL OR ");
            sb.append(stat.GetColumnName("adminId"));
            sb.append(" = ? ");
            conditions.add(sb.toString());
            args.add(adminId);
        }
        if (startTime != null) {
            conditions.add(stat.GetColumnName("createdAt") + " >= ? ");
            args.add(startTime);
        }
        if (endTime != null) {
            conditions.add(stat.GetColumnName("createdAt") + " <= ? ");
            args.add(endTime);
        }
        return countByMultiConditions(conditions, args);
    }

}
